//
//  ViewController.m
//  购物车
//
//  Created by 胡伟 on 16/10/10.
//  Copyright © 2016年 huwei. All rights reserved.
//

#import "ViewController.h"
#import "HWGoodsModel.h"
#import "HWTableViewCell.h"
#import "HWViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,HWTableViewCellDelegate>

@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,assign)float allPrice;
@property(nonatomic,strong)NSMutableArray * infoArr;

@property(nonatomic,strong)UIButton * allSelectBtn;
//总价格
@property(nonatomic,strong)UILabel * allPriceLab;
@end

@implementation ViewController
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    }
    return _tableView;
}
-(NSMutableArray *)infoArr{
    if (!_infoArr) {
        _infoArr = [NSMutableArray array];
    }
    return _infoArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"购物车";
    [self.view addSubview:self.tableView];
    //价格初始化为0
    self.allPrice = 0.0;
    for (int i = 0; i < 6; i++) {
        NSMutableDictionary * infoDict = [NSMutableDictionary dictionary];
        [infoDict setValue:@"img6" forKey:@"imageName"];
        [infoDict setValue:@"这是商品标题" forKey:@"goodsTitle"];
        [infoDict setValue:@"2000" forKey:@"goodsPrice"];
        [infoDict setValue:[NSNumber numberWithBool:NO] forKey:@"selectState"];
        [infoDict setValue:[NSNumber numberWithInt:1] forKey:@"goodsNum"];
        //封装数据模型
        HWGoodsModel * model = [[HWGoodsModel alloc]initWithDict:infoDict];
        [self.infoArr addObject:model];
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [self creatFooter];
}
//创建尾部尾部视图
-(UIView*)creatFooter{
    UIView * footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 150)];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.bounds.size.width - 150, 10, 50, 30)];
    label.text = @"全选";
    [footerView addSubview:label];
    //设置全选
    self.allSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.allSelectBtn.frame = CGRectMake(self.view.bounds.size.width - 100, 10, 30, 30);
    [self.allSelectBtn setImage:[UIImage imageNamed:@"复选框-未选中"] forState:UIControlStateNormal];
    [self.allSelectBtn addTarget:self action:@selector(allSelectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:self.allSelectBtn];
    //添加小结文本框
    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(self.view.bounds.size.width - 150, 40, 60, 30)];
    lab.text = @"小结:";
    lab.textColor = [UIColor redColor];
    [footerView addSubview:lab];
    //总价格
    self.allPriceLab = [[UILabel alloc]initWithFrame:CGRectMake(self.view.bounds.size.width - 100, 40, 100, 30)];
    self.allPriceLab.text = @"0.0";
    self.allPriceLab.textColor = [UIColor redColor];
    [footerView addSubview:self.allPriceLab];
    //结算按钮
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 80, self.view.bounds.size.width, 30);
    [btn setTitle:@"去结算" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor blueColor];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(settleBtn) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:btn];
    return footerView;
}
//结算按钮点击事件
-(void)settleBtn{
    HWViewController * vc = [[HWViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
}
//计算总价格
-(void)totalPrice{
    //遍历数据源
    for (int i = 0; i < self.infoArr.count; i++) {
        HWGoodsModel * model = [self.infoArr objectAtIndex:i];
        //如果选中的就计算价格
        if (model.selectState) {
            self.allPrice = self.allPrice + model.goodsNum * [model.goodsPrice intValue];
        }
    }
    //赋值
    self.allPriceLab.text = [NSString stringWithFormat:@"%.2f",self.allPrice];
    //清0
    self.allPrice = 0.0;
}
//全选按钮
-(void)allSelectBtnClick:(UIButton*)sender{
    //判断是否选中  是改为否  否改为是
    sender.tag = !sender.tag;
    if (sender.tag) {
        [sender setImage:[UIImage imageNamed:@"复选框-选中"] forState:UIControlStateNormal];
    }else{
        [sender setImage:[UIImage imageNamed:@"复选框-未选中"] forState:UIControlStateNormal];
    }
    //改变单元格选中的状态
    for (int i = 0; i < self.infoArr.count; i++) {
        HWGoodsModel * model = [self.infoArr objectAtIndex:i];
        model.selectState = sender.tag;
    }
    //计算价格
    [self totalPrice];
    //刷新列表
    [self.tableView reloadData];
}
//返回单元格
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.infoArr.count;
}
//制定单元格内容
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cellID";
    HWTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[HWTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.delegate = self;
    }
    [cell addTheValue:self.infoArr[indexPath.row]];
    return cell;
}

//实现按钮的加减代理方法
-(void)btnClick:(UITableViewCell*)cell andfFlag:(int)flag{
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    switch (flag) {
        case 11:
        {
            //做减法
            //获取当前数据源内容 修改数据源内容 刷新
            HWGoodsModel * model = self.infoArr[indexPath.row];
            if (model.goodsNum > 1) {
                model.goodsNum--;
            }
        }
            break;
            
        case 12:
        {
            //做加法
            //获取当前数据源内容 修改数据源内容 刷新
            HWGoodsModel * model = self.infoArr[indexPath.row];
            model.goodsNum++;
        }
            break;
        default:
            break;
    }
    
    //刷新表格
    [self.tableView reloadData];
    //计算价格
    [self totalPrice];
}

//点击某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HWGoodsModel * model = self.infoArr[indexPath.row];
    if (model.selectState) {
        model.selectState = NO;
    }else{
        model.selectState = YES;
    }
    //刷新当前行
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    //计算价格
    [self totalPrice];
}
//设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
